package com.bw.zg2.view.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.zg2.R
import com.bw.zg2.base.BaseActivity2
import com.bw.zg2.databinding.ActivityLoginBinding
import com.bw.zg2.utils.DbUtils
import com.bw.zg2.utils.SpUtil
import com.bw.zg2.viewmodel.LoginViewModel

/**
 *
 * @ClassName: LoginActivity
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/12 11:54 AM
 * @Version: 1.0
 */
@Route(path = "/login/LoginActivity")
class LoginActivity :
    BaseActivity2<LoginViewModel, ActivityLoginBinding>(R.layout.activity_login, LoginViewModel()) {
    override fun initData() {
        viewModel.loginLiveData.observe(this) {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
                //获取登录成功返回的token
                SpUtil.putString("name", it.data.username)
                //接口性别字段返回为空
                SpUtil.putString("sex", "男")
                SpUtil.putString("icon", it.data.icon)
                SpUtil.putInt("money", 100000)
                val token = it.data.token
                SpUtil.putString("token", token)

                //登录成功，保存用户信息到 数据库
                it.data.money = 100000f
                DbUtils.shopDB.getLoginBeanDao().insert(it.data)

                //EventBus.getDefault().post("login")

                ARouter.getInstance().build("/main/MainActivity").navigation()
                finish()
            } else {
                //登录失败跳转注册页面
                startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
            }
        }


        //在作用域中接收 flow 的返回值
//        lifecycleScope.launch{
//            viewModel.loginSharedFlow.collect(){
//                ToastUtils.showShort(it.message)
//                if (it.code == 200) {
//                    //获取登录成功返回的token
//                    val token = it.data.token
//                    SpUtil.putString("token", token)
////                SPUtils.getInstance().put("token", it.data.token)
//                    ARouter.getInstance().build("/main/MainActivity").navigation()
//                } else {
//                    //登录失败跳转注册页面
//                    startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
//                }
//            }
//        }


    }

    override fun initView(savedInstanceState: Bundle?) {

        val stringName = SPUtils.getInstance().getString("name");
        val stringPass = SPUtils.getInstance().getString("pass");

        // 从本地 sp中获取 name,pass 如果不为空，则设置回现
        if (!TextUtils.isEmpty(stringName)) {
            dataBinding.etName.setText(stringName)
        }
        if (!TextUtils.isEmpty(stringPass)) {
            dataBinding.etPass.setText(stringPass)
        }


        dataBinding.btn.setOnClickListener {
            val phone = dataBinding.etName.text.toString()
            val pass = dataBinding.etPass.text.toString()

            //定义手机号的正则表达式

            //[35789] 号段
            //  \\d{9} 表示，9位数字
            val rgStr = "^1[356789]\\d{9}$"
            //
//            if (!phone.matches(Regex(rgStr))) {
//                ToastUtils.showShort("手机号格式不正确")
//                return@setOnClickListener
//            }

            if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pass)) {
                ToastUtils.showShort("用户名或密码不能为空")
                return@setOnClickListener
            }

            val map = HashMap<String, String>()
            map["username"] = phone
            map["password"] = pass

            viewModel.login(map)
        }

        dataBinding.tvRegister.setOnClickListener {

            startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
        }
    }


}

